<?php

    if(!defined('BASEPATH'))
        exit('No direct script access allowed');

    /**
     * SimpleLoginSecure Class
     *
     * Makes authentication simple and secure.
     *
     * Simplelogin expects the following database setup. If you are not using 
     * this setup you may need to do some tweaking.
     *
     */
    class simple_login
    {

        private $CI;
        private $user_table='users';
        private $nicename;
        private $email;
        private $password;
        private $password_text;
        private $registered;
        private $id;
        private $ac_type;
        private $objects_ID=-1;

        public function getNicename()
        {
            return $this->nicename;

        }

        public function setNicename($nicename)
        {
            $this->nicename=$nicename;

        }

        public function getEmail()
        {
            return $this->email;

        }

        public function setEmail($email)
        {
            $this->email=$email;

        }

        public function getPassword()
        {
            return $this->password;

        }

        public function setPassword($password)
        {
            $this->password=$password;

        }
        
        public function getPasswordText()
        {
            return $this->password_text;

        }

        public function setPasswordText($password)
        {
            $this->password_text=$password;

        }

        public function getId()
        {
            return $this->id;

        }

        public function setId($id)
        {
            $this->id=$id;

        }

        public function getAc_type()
        {
            return $this->ac_type;

        }

        public function setAc_type($ac_type)
        {
            $this->ac_type=$ac_type;

        }

        public function getObjects_ID()
        {
            return $this->objects_ID;

        }

        public function setObjects_ID($objects_ID)
        {
            $this->objects_ID=$objects_ID;

        }
        
        

        function __construct()
        {
            $this->CI=& get_instance();
            $this->registered=time();

        }

        /**
         * Create a user account
         *
         * @access	public
         * @return	bool
         */
        function create()
        {

            if(!isset($this->email) || $this->email == "")
                return FALSE;


            if(!isset($this->password) || $this->password == "")
                return FALSE;

            if($this->ac_type != 'agency' && $this->ac_type != 'hotel')
                return FALSE;

            //Insert account into the database
            $data=array(
                'nicename' => $this->nicename,
                'email' => $this->email,
                'password' => md5($this->password),
                'password_text' => $this->password,
                'registered' => time(),
                'ac_type' => $this->ac_type,
                'objects_ID' => $this->objects_ID
            );

            if(!$this->CI->db->insert($this->user_table, $data)) //There was a problem! 
                return false;

            return true;

        }

        /**
         * Login and sets session variables
         *
         * @access	public
         * @param	string
         * @param	string
         * @return	bool
         */
        function login($user_email='', $user_pass='')
        {
            $debug=FALSE;

            if($debug):
                $this->CI->firephp->log('User email:'.$user_email);
                $this->CI->firephp->log('User pass :'.$user_pass);
            endif;


            if($debug)
                $this->CI->firephp->log('method login');

            if($user_email == '' OR $user_pass == '')
                return false;

            if($debug)
                $this->CI->firephp->log('user mail and pass exists');

            //Check if already logged in
            if($this->CI->session->userdata('email') == $user_email)
                return true;

            if($debug)
                $this->CI->firephp->log('alredy loged in FALSE');

            //Check against user table
            $this->CI->db->where('email', $user_email);
            $query=$this->CI->db->get_where($this->user_table);


            if($debug)
                $this->CI->firephp->log('num_rows:'.$query->num_rows());

            if($query->num_rows() > 0)
            {

                $user_data=$query->row();

                if($debug):
                    $this->CI->firephp->log('user_pass:'.$user_pass);
                    $this->CI->firephp->log('user_pass md5:'.md5($user_pass));
                    $this->CI->firephp->log('DB pass md5:'.$user_data->password);
                endif;


                if(md5($user_pass) != $user_data->password)
                    return false;

                //Destroy old and create a fresh, brand new session
                $this->CI->session->sess_destroy();
                $this->CI->session->sess_create();

                // update login data IP,last_login and ever_login
                //$this->CI->db->simple_query('UPDATE '.$this->user_table.' SET lastlogin = '.time().', lastip = "'.$this->CI->input->ip_address().'" ever_login = TRUE  WHERE id = '.$user_data->ID);
                //Set session data
                $session_data['email']=$user_data->email;
                $session_data['nicename']=$user_data->nicename;
                $session_data['logged_in']=TRUE;
                $session_data['id']=$user_data->ID;
                $session_data['objects_ID']=$user_data->objects_ID;
                $session_data['ac_type']=$user_data->ac_type;

                $this->CI->session->set_userdata($session_data);

                return true;
            }
            else
            {
                return false;
            }

        }

        /**
         * Logout user
         *
         * @access	public
         * @return	void
         */
        function logout()
        {
            $this->CI->session->sess_destroy();

        }

        /**
         * Delete user
         *
         * @access	public
         * @return	bool
         */
        function delete()
        {

            if(!is_numeric($this->id))
                return false;

            return $this->CI->db->delete($this->user_table, array('ID' => $this->id));

        }

        /**
         * Reset user password
         *
         * @access	public
         * @return	bool
         */
        public function resetPassword()
        {
            if(!is_numeric($this->id))
                return false;

            $this->CI->db->where('ID', $this->id);
            return $this->CI->db->update($this->user_table, array('password' => md5("123456789")));

        }

        /**
         * Reset user password
         *
         * @access	public
         * @return	bool
         */
        public function changePassword($newPassword)
        {
            if(!is_numeric($this->id))
                return false;

            $this->CI->db->where('ID', $this->id);
            return $this->CI->db->update($this->user_table, array('password' => md5($newPassword)));

        }

    }

?>
